package com.baidu.structure.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.structure.R;
import com.baidu.structure.bean.ContactGroupBean;

import java.util.ArrayList;


/**
 * Created by sunmo on 2016/11/28.
 */
public class ContactAdapter extends BaseExpandableListAdapter {
    private ArrayList<ContactGroupBean> menuData;
    private Context context;

    public ContactAdapter(ArrayList<ContactGroupBean> menuData, Context context) {
        this.menuData = menuData;
        this.context = context;
    }

    @Override

    public int getGroupCount() {
        return menuData == null ? 0 : menuData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return menuData.get(groupPosition).getChildList() == null ? 0 : menuData.get(groupPosition).getChildList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return menuData.get(groupPosition).getName();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return menuData.get(groupPosition).getChildList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView tv;
        if (convertView == null) {
            tv = new TextView(context);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
            tv.setLayoutParams(params);
            tv.setPadding(100, 0, 0, 0);
        } else {
            tv = (TextView) convertView;
        }
        tv.setText(menuData.get(groupPosition).getName());
        return tv;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_contact, parent, false);
            holder.iconIv = (ImageView) convertView.findViewById(R.id.icon_view);
            holder.nameTv = (TextView) convertView.findViewById(R.id.name_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iconIv.setImageResource(menuData.get(groupPosition).getChildList().get(childPosition).getIcon());
        holder.nameTv.setText(menuData.get(groupPosition).getChildList().get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class ViewHolder {
        TextView nameTv;
        ImageView iconIv;
    }
}
